package com.example.demo.controller;


import com.example.demo.pojo.Bill;
import com.example.demo.pojo.Book;
import com.example.demo.service.BillService;
import com.example.demo.service.BookService;
import com.example.demo.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@ComponentScan
public class BillController {
    @Autowired
    private BillService billService;

    @Autowired
    private BookService bookService;

    @Autowired
    private CustomerService customerService;

   //---------------以下是退房结账
    @Transactional
    @RequestMapping("/admin-bill-book-get")
    public String getBookByCustNum(Model model,String custNum){
        System.out.println(custNum);
        List<Book> bookList = bookService.getBookByCustNum(custNum);
        String tishi="以下是查询 客户证件号为：" + custNum + "  的结果";
        model.addAttribute("tishi",tishi);
        model.addAttribute("bookList",bookList);
        return "/admin/admin-cust-ruzhu";
    }
    @Transactional
    @RequestMapping("/admin-bill-add")
    public String getcheckCustNum(Model model,String bookID){
        //获得订单，进入检查页面，显示订单数据，提供选择栏，可选择两名用户进行登记（custnum+custName+custPhone）
        Book book = bookService.getBookByBookID(Integer.parseInt(bookID));
        Bill bill = billService.getbillBybookID(bookID);
        String custName = customerService.getcustNameByNum(book.getCustNum());
        int custPhone = customerService.getcustPhoneByNum(book.getCustNum());
        model.addAttribute("book",book);
        model.addAttribute("bill",bill);
        model.addAttribute("custName",custName);
        model.addAttribute("custPhone",custPhone);
        return "/admin/admin-cust-ruzhu-add";
    }

    @Transactional
    @RequestMapping("/admin-bill-book-xiugai")
    public String getupdateByBookID(Model model,String bookID)throws Exception{
        //这个是用于获得book便于查看页面
        Book book = bookService.getBookByBookID(Integer.parseInt(bookID));
        model.addAttribute("book",book);
        return "/admin/admin-book-xiugai";
    }

    @Transactional
    @RequestMapping("/admin-bill-book-delete")
    @ResponseBody
    public String deleteBookByBookId(String bookID) {
        int flag = bookService.deleteBookByBookId(Integer.parseInt(bookID));
        if (flag ==1)
            return "<script type='text/javascript'>alert('删除成功');window.location='/admin-cust-ruzhu'</script>";
        else
            return "<script type='text/javascript'>alert('删除失败');window.location='/admin-cust-ruzhu'</script>";
    }

    @RequestMapping("/admin-bill-book-delete-tj")
    @ResponseBody
    public String delete_tj(String bookID) {
        return "<script type='text/javascript'> var flag=  " +
                "window.confirm('确认删除吗');" +
                "if(flag == true)  " +
                "window.location='/admin-bill-book-delete?bookID="+ bookID +
                "';else  " +
                "  window.location='/admin-cust-ruzhu' ;</script>";
    }



    @RequestMapping("/admin-bill-ty")
    @ResponseBody
    public String addBill(String bookID) {
        //确定入住，修改订单状态、创建bill
        return "<script type='text/javascript'> var flag=  " +
                "window.confirm('确认删除吗');" +
                "if(flag == true)  " +
                "window.location='/admin-bill-book-delete?bookID="+ bookID +
                "';else  " +
                "  window.location='/admin-bill-book-get' ;</script>";
    }
    //--------------------------------------以下是入住登记
    @RequestMapping("/admin-cust-ruzhu")
    public String getadminruzhu(){
        return "/admin/admin-cust-ruzhu";
    }

    @Transactional
    @RequestMapping("/admin-cust-ruzhu-check")
    @ResponseBody
    public String checkCustNum(int billID,String bookID,String roomID, String custNum,String custName,
                               String custPhone,String custNum2,String custName2,String custPhone2,String ddd,String deposit,String billMoney){
        //进入检查页面，显示订单数据，提供选择栏，可选择两名用户进行登记（custnum+custName+custPhone）
        //登记完custNum，在customer中用户的入住状态，如果没有该用户，就创建一个customer，并且顾客表中的roomID为入住房号
        if(custNum != custNum2) {
            int custState = 1;

            System.out.println(custState + "," + billID + "," + bookID + "," + roomID + "," + custNum + "," + custName + "," +
                    custPhone + "," + custNum2 + "," + custName2 + "," + custPhone2 + "," + ddd + "," + deposit + "," + billMoney);

            billService.checkCustNum(custState, billID, bookID, roomID, custNum, custName, custPhone, custNum2, custName2, custPhone2, ddd, deposit, billMoney);

            return "<script type='text/javascript'>alert('登记入住成功！');window.location='/admin-cust-ruzhu'</script>";
        }else {
            return "<script type='text/javascript'>alert('两个用户的身份证相同，请重新登记！');window.location='/admin-cust-ruzhu'</script>";
        }
    }


    @RequestMapping("/admin-cust-out")
    public String getcustOut() {
        return "/admin/admin-cust-out";
    }

    @Transactional
    @RequestMapping("/admin-cust-out-get")
    public ModelAndView custOutUpdate( String roomID) {
        System.out.println(roomID);
        //获得了房号，查询bill表单得到bill对象实体
        List<Bill>  billList = billService.getbillByRoomID(roomID);
        if(billList == null){
            return new ModelAndView("redirect:/admin-cust-out-get-false");
        }else{
            //model.addAttribute("bill",bill);
            ModelAndView mav = new ModelAndView();
            String tishi = "以下是该房间号:   "+ roomID +"    的账单：";
            mav.addObject("tishi",tishi);
            mav.addObject("billList",billList);
            mav.setViewName("/admin/admin-cust-out-get");
            return mav;
        }
    }
    @RequestMapping("/admin-cust-out-get-success")
    public String admincustoutgetsuccess() {
        return "/admin/admin-cust-out";
    }

    @RequestMapping("/admin-cust-out-get-false")
    @ResponseBody
    public String admincustoutgetfalse() {
        return  "<script type='text/javascript'>alert('不存在该房间号');window.location='/admin-cust-out'</script>";
    }

    @Transactional
    @RequestMapping("/admin-cust-out-update")
    public String getcustOutupdate(Model model,String billID) {
        //获得了订单号，编辑状态
        Bill bill = billService.getbillBybillID(billID);
        model.addAttribute("bill",bill) ;
        return "/admin/admin-cust-out-update";
    }

    @Transactional
    @RequestMapping("/admin-cust-out-chuli")
    @ResponseBody
    public String custOutUpdatechuli(String billID,String billState) {
        //结账处理，输入房号：显示：bill信息，便于核对，显示付款状态以及押金金额，退押金

        Bill bill = billService.getbillBybillID(billID);
        billService.updateBillState(billID,billState);
        customerService.updateCustStateBy(bill.getCustNum(),0);
        customerService.updateCustStateBy(bill.getCustNum2(),0);
        bookService.updateBookStateByBookId(3,bill.getBookID());

        //退房处理：已退房，改billstate， 改用户入住状态，改订单状态
        return "<script type='text/javascript'>alert('结账成功');window.location='/admin-cust-out'</script>";
    }




    @Transactional
    @RequestMapping("/Admin/countForRoomRental")
    @ResponseBody
    public int countForRoomRental(Model model) {
        int countForRoomRental =billService.countForRoomRental();
        model.addAttribute("countForRoomRental",countForRoomRental);
        return countForRoomRental;
    }
    @Transactional
    @RequestMapping("/Admin/countForCustLiving")
    @ResponseBody
    public int countForCustLiving(Model model) {
        int countForCustLiving =billService.countForCustLiving();
        model.addAttribute("countForCustLiving",countForCustLiving);
        return countForCustLiving;
    }
}
